<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";


class PagingSnippet extends IPlugin 
{
        function PagingSnippet($parent = null)
        {
                IPlugin::IPlugin($parent);
                $this->tplfolder = dirname(__FILE__)."/tpl";
        }

        function getPluginTag() { return "paging"; }
        function getPluginName() { return "Create Pagination of defined data snippet"; }
        function getPluginVersion() { return "1.0.0"; }

        function Render($params = null)
        {
//"items" => "",
//"source" => "sql", // -- sql, array
//"sql_count" => "select count(*) from subscribe",
//"ippg" => 10, // -- items per page
//"nppv" => 5, // -- number pages per view
//"urlbase" => "?path=orders
//"page" => &$VAR => return current page
//"ppg" => &$VAR  => return current ppv values

                if($params['noseolinks'] == true){
                        return $this->noseorender($params);

                        }

                $core = getCoreInstance();
                $framework = $core->getProperty("framework");
                $render = $framework->smarty;

                $page = $_GET["st_paging_page"];
                if($page==-1)
                {
                        $page = 0;
                        $params["ippg"] = 1000000;
                } else 
                if(!($page>=0))
                {
                        $page = 0;
                }
                $ippg = $params["ippg"];
                $ppv = $_GET["st_paging_ppv"];
                if(!($ppv>=0))
                {
                        $ppv = 0;
                }
        
                if($ppv>(ceil(($page+1)/$params["nppv"])-1))
                {
                        $ppv = ceil(($page+1)/$params["nppv"])-1;
                }

                $dtype = "";
                $dcnt = -1;
                $data = array();
                switch($params["source"])
                {
                        case "sql": {
                                $dtype = "sql";
                                $db = &$core->getProperty("db");
                                $dcnt = $db->getOne($params["sql_count"]);
                        } break;
                        case "array": {
                                $dtype = "array";
                                $dcnt = count($params["items"]);
                        } break;
                };
                #NEW
                $class = split('=',$params['urlbase']);     
                $class = str_replace('&cid','',str_replace('&page','',$class[1]));     
                $pid   = split('&cid=',$params['urlbase']);
                $pid   = $pid[1];
                        
                #echo $class.'-'.$pid;
                $npg = ceil($dcnt/$params["ippg"]);
                $pages = array();
                for($i=$ppv*$params["nppv"]; $i<($ppv+1)*$params["nppv"]; $i++)
                {
                        if($i>=$npg) break;
                        #$pages[$i+1] = $i == $page ? "" : $params["urlbase"]."&st_paging_page={$i}&st_paging_ppv=".(ceil(($i+1)/$params["nppv"])-1);
                        $pages[$i+1] = $i == $page ? "" : '/Paging-'.$class.'-'.$pid."-{$i}-".(ceil(($i+1)/$params["nppv"])-1).'.html';
                        #echo $params['urlbase'];
                }
                $render->assign("pi_pageslist", $pages);

                if($page>0 && $ppv>0)
                {
                        $render->assign("pi_ppvlist_prev", $params["urlbase"]."&st_paging_page=".(($ppv*$params["nppv"])-1)."&st_paging_ppv=".($ppv-1));
                }
                #$render->assign("pi_pageslist_prev", $page>0 ? $params["urlbase"]."&st_paging_page=".($page-1)."&st_paging_ppv=".((ceil($page/$params["nppv"])-1)<$ppv ? $ppv-1 : $ppv) : "");

                $pi_pageslist_prev = $page>0 ? '/Paging-'.$class.'-'.$pid."-".($page-1).'-'.(   (ceil(($page)/$params["nppv"])-1)>$ppv ? $ppv-1 : $ppv) : "";
                if(!empty($pi_pageslist_prev)){
                $pi_pageslist_prev .='.html';
                }
                $render->assign("pi_pageslist_prev", $pi_pageslist_prev);

                #$render->assign("pi_pageslist_next", ($page+1)<$npg ? $params["urlbase"]."&st_paging_page=".($page+1)."&st_paging_ppv=".(   (ceil(($page+2)/$params["nppv"])-1)>$ppv ? $ppv+1 : $ppv) : "");

                $pi_pageslist_next = ($page+1)<$npg ? '/Paging-'.$class.'-'.$pid."-".($page+1).'-'.(   (ceil(($page+2)/$params["nppv"])-1)>$ppv ? $ppv+1 : $ppv) : "";
                if(!empty($pi_pageslist_next)){
                $pi_pageslist_next .='.html';
                }
                $render->assign("pi_pageslist_next", $pi_pageslist_next);

                if(!$npg)
                {
                        $nnpg = 0;
                }
                else
                {
                        $nnpg = ceil($npg/$params["nppv"])-1;
                }
                if($nnpg>0 && $ppv<$nnpg)
                {
                        $render->assign("pi_ppvlist_next", $params["urlbase"]."&st_paging_page=".(($ppv+1)*$params["nppv"])."&st_paging_ppv=".($ppv+1));
                }

                #$render->assign("pi_pageslist_all", ($npg>0 && $_GET["st_paging_page"]!=-1) ? $params["urlbase"]."&st_paging_page=-1" : "" );
                $render->assign("pi_pageslist_all", ($npg>0 && $_GET["st_paging_page"]!=-1) ? '/Paging-'.$class.'-'.$pid."ALL.html" : "" );
                #$render->assign("pi_pageslist_pagged", ($npg>0 && $_GET["st_paging_page"]==-1) ? $params["urlbase"]."&st_paging_page=0&st_paging_ppv=0" : "" );
                $render->assign("pi_pageslist_pagged", ($npg>0 && $_GET["st_paging_page"]==-1) ? '/Paging-'.$class.'-'.$pid."-"."0-0.html" : "" );
                $result = $render->fetch("file:".$this->tplfolder."/page.tpl.php");
                
                // -- return page values
                if(isset($params["page"]) && isset($params["ppg"]))
                {
                        if (1000000 == $params["ippg"])
                        {
                                $params["page"] = -1;
                                $params["ppg"] = -1;
                        }
                        else
                        {
                                $params["page"] = $page;
                                $params["ppg"] = $ppv;
                        }
                }
                
                return $result;
        }


        function noseorender($params = null){

                $core = getCoreInstance();
                $framework = $core->getProperty("framework");
                $render = $framework->smarty;

                $page = $_GET["st_paging_page"];
                if($page==-1)
                {
                        $page = 0;
                        $params["ippg"] = 1000000;
                } else 
                if(!($page>=0))
                {
                        $page = 0;
                }
                $ippg = $params["ippg"];
                $ppv = $_GET["st_paging_ppv"];
                if(!($ppv>=0))
                {
                        $ppv = 0;
                }
        
                if($ppv>(ceil(($page+1)/$params["nppv"])-1))
                {
                        $ppv = ceil(($page+1)/$params["nppv"])-1;
                }

                $dtype = "";
                $dcnt = -1;
                $data = array();
                switch($params["source"])
                {
                        case "sql": {
                                $dtype = "sql";
                                $db = &$core->getProperty("db");
                                $dcnt = $db->getOne($params["sql_count"]);
                        } break;
                        case "array": {
                                $dtype = "array";
                                $dcnt = count($params["items"]);
                        } break;
                };

                $npg = ceil($dcnt/$params["ippg"]);
                $pages = array();
                for($i=$ppv*$params["nppv"]; $i<($ppv+1)*$params["nppv"]; $i++)
                {
                        if($i>=$npg) break;
                        $pages[$i+1] = $i == $page ? "" : $params["urlbase"]."&st_paging_page={$i}&st_paging_ppv=".(ceil(($i+1)/$params["nppv"])-1);
                }
                $render->assign("pi_pageslist", $pages);

                if($page>0 && $ppv>0)
                {
                        $render->assign("pi_ppvlist_prev", $params["urlbase"]."&st_paging_page=".(($ppv*$params["nppv"])-1)."&st_paging_ppv=".($ppv-1));
                }
                $render->assign("pi_pageslist_prev", $page>0 ? $params["urlbase"]."&st_paging_page=".($page-1)."&st_paging_ppv=".((ceil($page/$params["nppv"])-1)<$ppv ? $ppv-1 : $ppv) : "");

                $render->assign("pi_pageslist_next", ($page+1)<$npg ? $params["urlbase"]."&st_paging_page=".($page+1)."&st_paging_ppv=".(   (ceil(($page+2)/$params["nppv"])-1)>$ppv ? $ppv+1 : $ppv) : "");

                if(!$npg)
                {
                        $nnpg = 0;
                }
                else
                {
                        $nnpg = ceil($npg/$params["nppv"])-1;
                }
                if($nnpg>0 && $ppv<$nnpg)
                {
                        $render->assign("pi_ppvlist_next", $params["urlbase"]."&st_paging_page=".(($ppv+1)*$params["nppv"])."&st_paging_ppv=".($ppv+1));
                }

                $render->assign("pi_pageslist_all", ($npg>0 && $_GET["st_paging_page"]!=-1) ? str_replace('/?path','/index.php?path',$params["urlbase"])."&st_paging_page=-1" : "" );
                $render->assign("pi_pageslist_pagged", ($npg>0 && $_GET["st_paging_page"]==-1) ? str_replace('/?path','/index.php?path',$params["urlbase"])."&st_paging_page=0&st_paging_ppv=0" : "" );
                $result = $render->fetch("file:".$this->tplfolder."/page.tpl.php");
                
                // -- return page values
                if(isset($params["page"]) && isset($params["ppg"]))
                {
                        if (1000000 == $params["ippg"])
                        {
                                $params["page"] = -1;
                                $params["ppg"] = -1;
                        }
                        else
                        {
                                $params["page"] = $page;
                                $params["ppg"] = $ppv;
                        }
                }
                
                return $result;
}

};
?>